package com.hbean.service.impl;

import com.hbean.entity.SysRoleMenu;
import com.hbean.mapper.SysRoleMenuMapper;
import com.hbean.service.SysRoleMenuService;
import com.hbean.utils.Condition;
import com.hbean.utils.HUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: null
 * @Project: hbean
 * @Author: hjibo
 * @CreateDate: 2018/10/30 15:57
 * @ModificationHistory: （who whatTime doWhat）
 */

@Service
public class SysRoleMenuServiceImpl implements SysRoleMenuService {

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    @CachePut(value = "sysCache", key="'sysRoleMenu'") //每次都会执行方法，并将结果存入指定的缓存中
    public List<SysRoleMenu> insertDataAndUpdateCache(SysRoleMenu sysRoleMenu) {

        this.sysRoleMenuMapper.deleteData(sysRoleMenu);

        List<String> strings = HUtil.stringToList(sysRoleMenu.getTemp2(),",");
        List<SysRoleMenu> sysRoleMenus = new ArrayList<>();
        strings.forEach(s -> {
            SysRoleMenu sysRoleMenu1 = new SysRoleMenu();
            sysRoleMenu1.setSrm00(HUtil.uuidOf18());
            sysRoleMenu1.setSrm01(sysRoleMenu.getSrm01());
            sysRoleMenu1.setSrm02(s);
            sysRoleMenus.add(sysRoleMenu1);
        });
        this.sysRoleMenuMapper.insertDatas(sysRoleMenus);

        return this.sysRoleMenuMapper.selectDatas(new Condition());
    }

    @Override
//    @CachePut(value = "sysCache", key="'sysRoleMenu'") //每次都会执行方法，并将结果存入指定的缓存中
    public List<SysRoleMenu> deleteDataAndUpdateCache(SysRoleMenu sysRoleMenu) {
        return null;
    }

    @Override
//    @CachePut(value = "sysCache", key="'sysRoleMenu'") //每次都会执行方法，并将结果存入指定的缓存中
    public List<SysRoleMenu> updateDataAndUpdateCache(SysRoleMenu sysRoleMenu) {
        return null;
    }

    @Override
    @Cacheable(value = "sysCache", key="'sysRoleMenu'") //开启缓存策略
    public List<SysRoleMenu> getDataForCache() {
        return this.sysRoleMenuMapper.selectDatas(new Condition());
    }

}
